From 20ce682e856952bd3f2cbaa18a690e531bdce024 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 18 May 2015 14:55:42 -0700 Subject: [PATCH] Recognize RUSTC/RUSTDOC environment variables --- src/cargo/ops/cargo_rustc/context.rs | 2 +- src/cargo/ops/cargo_rustc/engine.rs | 6 +++--- src/cargo/ops/cargo_rustc/mod.rs | 2 +- src/cargo/util/config.rs | 8 ++++++++ src/cargo/util/mod.rs | 2 +- src/doc/config.md | 15 ++++++++++----- tests/test_cargo_compile.rs | 23 +++++++++++++++++++++++ 7 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index cb52e9446..65e612f8f 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -98,7 +98,7 @@ impl<'a, 'b: 'a> Context<'a, 'b> { /// specified as well as the exe suffix fn filename_parts(target: Option<&str>) -> CargoResult<(Option<(String, String)>, String)> { - let mut process = try!(util::process("rustc")); + let mut process = try!(util::process(util::rustc())); process.arg("-") .arg("--crate-name").arg("_") .arg("--crate-type").arg("dylib") diff --git a/src/cargo/ops/cargo_rustc/engine.rs b/src/cargo/ops/cargo_rustc/engine.rs index 75c467217..3d3a17d8b 100644 --- a/src/cargo/ops/cargo_rustc/engine.rs +++ b/src/cargo/ops/cargo_rustc/engine.rs @@ -4,7 +4,7 @@ use std::fmt; use std::path::Path; use std::process::Output; -use util::{CargoResult, ProcessError, ProcessBuilder, process}; +use util::{self, CargoResult, ProcessError, ProcessBuilder, process}; /// Trait for objects that can execute commands. pub trait ExecEngine: Send + Sync { @@ -38,8 +38,8 @@ impl CommandPrototype { pub fn new(ty: CommandType) -> CargoResult { Ok(CommandPrototype { builder: try!(match ty { - CommandType::Rustc => process("rustc"), - CommandType::Rustdoc => process("rustdoc"), + CommandType::Rustc => process(util::rustc()), + CommandType::Rustdoc => process(util::rustdoc()), CommandType::Target(ref s) | CommandType::Host(ref s) => process(s), }), diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 64e2fd275..5986693d0 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -57,7 +57,7 @@ pub struct TargetConfig { /// The second element of the tuple returned is the target triple that rustc /// is a host for. pub fn rustc_version() -> CargoResult<(String, String)> { - let output = try!(try!(util::process("rustc")) + let output = try!(try!(util::process(util::rustc())) .arg("-vV") .exec_with_output()); let output = try!(String::from_utf8(output.stdout).map_err(|_| { diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index a6479e818..f2a592530 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -394,6 +394,14 @@ fn homedir() -> Option { return cargo_home.or(user_home); } +pub fn rustc() -> String { + env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string()) +} + +pub fn rustdoc() -> String { + env::var("RUSTDOC").unwrap_or_else(|_| "rustdoc".to_string()) +} + fn walk_tree(pwd: &Path, mut walk: F) -> CargoResult<()> where F: FnMut(File, &Path) -> CargoResult<()> { diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index e29fa5844..ea71ae780 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -1,4 +1,4 @@ -pub use self::config::Config; +pub use self::config::{Config, rustc, rustdoc}; pub use self::process_builder::{process, ProcessBuilder}; pub use self::errors::{CargoResult, CargoError, ChainError, CliResult}; pub use self::errors::{CliError, ProcessError}; diff --git a/src/doc/config.md b/src/doc/config.md index d48c9dddd..32b6d3d51 100644 --- a/src/doc/config.md +++ b/src/doc/config.md @@ -79,9 +79,14 @@ timeout = 60000 # Timeout for each HTTP request, in milliseconds jobs = 1 # number of jobs to run by default (default to # cpus) ``` -# Configuration of registry cache +# Environment Variables -Cargo maintains a local cache of the registry index and of git -checkouts of crates. By default these are stored under -`$HOME/.cargo`. The location can be overridden by setting the -`CARGO_HOME` environment variable. +Cargo recognizes a few global environment variables to configure how it runs: + +* `CARGO_HOME` - Cargo maintains a local cache of the registry index and of git + checkouts of crates. By default these are stored under `$HOME/.cargo`, but + this variable overrides the location of this directory. +* `RUSTC` - Instead of running `rustc`, Cargo will execute this specified + compiler instead. +* `RUSTDOC` - Instead of running `rustdoc`, Cargo will execute this specified + `rustdoc` instance instead. diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index c8bd75f6a..0a50d770e 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1758,6 +1758,29 @@ test!(dashes_in_crate_name_bad { execs().with_status(101)); }); +test!(rustc_env_var { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build") + .env("RUSTC", "rustc-that-does-not-exist").arg("-v"), + execs().with_status(101) + .with_stderr("\ +Could not execute process `rustc-that-does-not-exist -vV` ([..]) + +Caused by: +[..] +")); + assert_that(&p.bin("a"), is_not(existing_file())); +}); + test!(filtering { let p = project("foo") .file("Cargo.toml", r#" -- 2.30.2